package com.readygo.boot_test.controller;

import ch.qos.logback.classic.Logger;
import com.readygo.boot_test.entity.Course;
import com.readygo.boot_test.entity.auth.SecurityUser;
import com.readygo.boot_test.entity.User;
import com.readygo.boot_test.request.AddSecurityUserRequest;
import com.readygo.boot_test.response.AddSecurityUserResponse;
import com.readygo.boot_test.service.CourseService;
import com.readygo.boot_test.service.auth.SecurityUserService;
import com.readygo.boot_test.service.UserService;
import com.readygo.boot_test.vo.UserCourse;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class HomeController {

    private final static Logger logger = (Logger) LoggerFactory.getLogger(HomeController.class);

    private CourseService courseService;
    private UserService userService;
    private SecurityUserService securityUserService;

    @Autowired
    public HomeController(CourseService courseService, UserService userService, SecurityUserService securityUserService) {

        this.userService = userService;
        this.courseService = courseService;
        this.securityUserService = securityUserService;

    }

//    @RequestMapping("/")
//    public Object home() {
//        logger.debug("test");
//        return "Ok";
//    }

    @RequestMapping(value = "/addUser", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> saveOrUpdateUser(@RequestBody User user) {
        User result = userService.saveOrUpdateUser(user);
        return new ResponseEntity<String>(result != null ? "success" : "failed", HttpStatus.OK);
    }

    @RequestMapping(value = "/addCourse", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> saveOrUpdateCourse(@RequestBody Course course) {
        Course result = courseService.saveOrUpdateCourse(course);
        return new ResponseEntity<String>(result != null ? "success" : "failed", HttpStatus.OK);
    }

    @RequestMapping(value = "/listCourse" ,method = RequestMethod.GET)
    public ResponseEntity<List<UserCourse>> getCourseListByUserId(@RequestParam("userId") Integer userId) {
        List<UserCourse> userCourseList = userService.getUserCourseList(userId);
        return new ResponseEntity<List<UserCourse>>(userCourseList, HttpStatus.OK);
    }

    @RequestMapping(value = "/addSecurityUser", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> addSecurityUser(@RequestBody AddSecurityUserRequest request) {
        ResponseEntity<?> responseEntity = null;
        SecurityUser securityUser = securityUserService.save(request);
        if (securityUser != null) {
            AddSecurityUserResponse response = new AddSecurityUserResponse();
            BeanUtils.copyProperties(securityUser, response);
            responseEntity = new ResponseEntity<AddSecurityUserResponse>(response, HttpStatus.OK);
        } else {
            responseEntity = new ResponseEntity<String>("error", HttpStatus.OK);
        }
        return responseEntity;
    }


}
